package com.che.software.testato.business;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.che.software.testato.business.exception.VariantCreationManagerException;
import com.che.software.testato.business.exception.VariantSearchManagerException;
import com.che.software.testato.business.exception.VariantUpdateManagerException;
import com.che.software.testato.domain.dao.IVariantDAO;
import com.che.software.testato.domain.dao.jdbc.exception.VariantCreationDAOException;
import com.che.software.testato.domain.dao.jdbc.exception.VariantSearchDAOException;
import com.che.software.testato.domain.dao.jdbc.exception.VariantUpdateDAOException;
import com.che.software.testato.domain.entity.Variant;
import com.che.software.testato.domain.entity.creation.VariantCreation;
import com.che.software.testato.domain.entity.search.VariantSearch;

/**
 * Manager dedicated to the variants.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @since July, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
@Service("variantManager")
public class VariantManager {

	/**
	 * Constants.
	 */

	private static final Logger LOGGER = Logger.getLogger(VariantManager.class);

	/**
	 * Members.
	 */
	@Autowired
	private IVariantDAO variantDAO;

	/**
	 * Checks if variants are already existing or not for a given test case.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param testCaseId the test case id.
	 * @return true if variants are already existing, else false.
	 * @since July, 2011.
	 * @throws VariantSearchManagerException if an error occurs during the
	 *         search.
	 */
	public boolean areVariantsExistingFromTestCaseId(int testCaseId) throws VariantSearchManagerException {
		LOGGER.debug("areVariantsExistingFromTestCaseId(" + testCaseId + ").");
		try {
			return variantDAO.areVariantsExistingFromTestCaseId(testCaseId);
		} catch (VariantSearchDAOException e) {
			throw new VariantSearchManagerException(e);
		}
	}

	/**
	 * Creates variants for a given test case. Is called after the variants
	 * generation.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param testCaseId the test case id.
	 * @param variants the list of generated variants.
	 * @since July, 2011.
	 * @throws VariantCreationManagerException if an error occurs during the
	 *         creation.
	 */
	public void createVariantsFromTestCaseId(int testCaseId, List<VariantCreation> variants) throws VariantCreationManagerException {
		LOGGER.debug("createVariantsFromTestCaseId(" + testCaseId + ", " + variants.size() + " variants).");
		try {
			variantDAO.createVariantsFromTestCaseId(testCaseId, variants);
		} catch (VariantCreationDAOException e) {
			throw new VariantCreationManagerException(e);
		}
	}

	/**
	 * Variant search from a bean of criterions.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param searchBean the criterions to use for the search.
	 * @return the resulting object list.
	 * @since July, 2011.
	 * @throws VariantSearchManagerException if an error occurs during the
	 *         search.
	 */
	public List<Variant> searchVariant(VariantSearch searchBean) throws VariantSearchManagerException {
		LOGGER.debug("searchVariant().");
		try {
			return variantDAO.searchVariant(searchBean);
		} catch (VariantSearchDAOException e) {
			throw new VariantSearchManagerException(e);
		}
	}

	/**
	 * Updates a variant from his object.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param variantToUpdate the variant object to update in the database.
	 * @since July, 2011.
	 * @throws VariantUpdateManagerException if an error occurs during the
	 *         update.
	 */
	public void updateVariant(Variant variantToUpdate) throws VariantUpdateManagerException {
		LOGGER.debug("updateVariant(" + variantToUpdate.getVariantId() + ").");
		try {
			variantDAO.updateVariant(variantToUpdate);
		} catch (VariantUpdateDAOException e) {
			throw new VariantUpdateManagerException(e);
		}
	}

	/**
	 * Getter for the private field value variantDAO.
	 * 
	 * @return the variantDAO field value.
	 */
	public IVariantDAO getVariantDAO() {
		return variantDAO;
	}

	/**
	 * Setting a value to the variantDAO field.
	 * 
	 * @param variantDAO the value to set.
	 */
	public void setVariantDAO(IVariantDAO variantDAO) {
		this.variantDAO = variantDAO;
	}
}